using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.Shell.Interop;
using OleConstants = Microsoft.VisualStudio.OLE.Interop.Constants;
using VsCommands = Microsoft.VisualStudio.VSConstants.VSStd97CmdID;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
using MH.WsdlWorks.ProjectModel;
using System.Collections.Generic;


namespace MH.WsdlWorks 
{ 
    [CLSCompliant(false)]    
    [ComVisible(true)]    
    [Guid("7B61EA22-333F-4b4e-A000-FB847CDD4A99")]    
    public class WsdlWorksProject : SimpleProjectNode 
    {
        public WsdlWorksProject()
            : base()
        {
        }

        public override Guid ProjectGuid 
        { 
            get 
            { 
                return typeof(WsdlWorksProjectFactory).GUID; 
            } 
        } 
        
        public override string ProjectType 
        { 
            get 
            { 
                return this.GetType().Name; 
            } 
        }

        // is this where menu items get supported?
        protected override int QueryStatusOnNode(
            Guid guidCmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                switch (cmd)
                {
                    case PackageCommandIdList.cmdidAddWsdl:
                    case PackageCommandIdList.cmdidAddRequest:
                        result |= QueryStatusResult.SUPPORTED | QueryStatusResult.ENABLED;
                        return VSConstants.S_OK;
                }
            }
            return base.QueryStatusOnNode(guidCmdGroup, cmd, pCmdText, ref result);
        }

        protected override int ExecCommandOnNode(Guid guidCmdGroup, uint cmd, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                if (cmd == PackageCommandIdList.cmdidAddWsdl)
                {
                    this.AddNewWsdlNode();
                    return VSConstants.S_OK;
                }
            }
            return base.ExecCommandOnNode(guidCmdGroup, cmd, nCmdexecopt, pvaIn, pvaOut);
        }

        /// <summary>
        /// This called from SimpleProjectNode.Reload() to create all the project items
        /// described by the tmproj file.
        /// </summary>
        protected override void LoadWsdlProjectItems()
        {
            foreach (Wsdl wsdl in this.WsdlProject.Wsdls)
            {
                AddWsdlNodeToProject(wsdl);
            }
        }

        /// <summary>
        /// Add a new wsdl node
        /// </summary>
        /// <returns></returns>
        void AddNewWsdlNode()
        {
            WsdlFromUrlResult result;
            using (WsdlFromUrlDialog wsdlFromUrlDialog = new WsdlFromUrlDialog())
            {
                result = wsdlFromUrlDialog.GetWsdl();
            }
            if (!result.Cancelled)
            {
                string nodeName = "Wsdl";
                Wsdl wsdl = new Wsdl(this.WsdlProject, nodeName, result.Url.ToString());
                this.WsdlProject.Wsdls.Add(wsdl);
                AddWsdlNodeToProject(wsdl);
            }
        }

        /// <summary>
        /// add a wsdl node to this project to represent the given Wsdl object
        /// </summary>
        /// <param name="wsdl"></param>
        void AddWsdlNodeToProject(Wsdl wsdl)
        {
            WsdlNode wsdlNode = new WsdlNode(this, wsdl);
            this.AddChild(wsdlNode);
        }

        // we don't need references for this project
        protected override ReferenceContainerNode CreateReferenceContainerNode()
        {
            return null;
        }

        private WsdlProject WsdlProject
        {
            get
            {
                if (this.BuildProject.WsdlProject == null)
                {
                    System.Diagnostics.Debug.Assert(false, "WsdlProject has not been created yet.");
                }
                return this.BuildProject.WsdlProject;
            }
        }

        /// <summary>
        /// Make sure we set the path of the WsdlProject to the project path
        /// </summary>
        protected override void InitializeProjectProperties()
        {
            WsdlProject.SetPath(this.FileName);
            base.InitializeProjectProperties();
        }
    } 
}